home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10739 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  68 lines

  1. Newsgroups: comp.lang.c++
  2. Path: nntp.coast.net!torn!news!apollo!saed
  3. From: saed@engn.uwindsor.ca (Saed Aryan,13325,1100,g)
  4. Subject: c++.moderated Q : template class as friend 
  5. X-Nntp-Posting-Host: apollo.engn.uwindsor.ca
  6. Message-ID: <Do0q95.C0o@news.uwindsor.ca>
  7. Keywords: friend, template
  8. Sender: news@news.uwindsor.ca (Usenet)
  9. Reply-To: saed@engn.uwindsor.ca
  10. Organization: VLSI Research Group - University of Windsor
  11. Date: Sat, 9 Mar 1996 20:58:17 GMT
  12.  
  13. Hi all,
  14.  
  15. magnus@darwin.uchicago.edu (Magnus Nordborg) wrote in c++.moderated : 
  16.  
  17. >How do I accomplish the following?  I have a class, baseA, from which I
  18. >derive
  19. >class A : public baseA.
  20. >I also have a template class, baseB, from which I derive
  21. >class B : public baseB<A>,
  22. >and B's members need access to the protected members of baseA.  In other
  23. >words, how does one declare a template class to be a "friend" of the class
  24. >used in the template instantiation? 
  25.  
  26. Well, my code below shows how to do that, and it compiles just fine
  27. (g++ 2.7.0).
  28.  
  29. But B is a _derived_ class of the template class. to me
  30. "In other words" doesn't comply with the class structure you show above.
  31. What is the role of the base and derived classes in your example?
  32.  
  33. Did you want the whole template class to be a friend of one template
  34. instance? If so then the more general question would be:
  35.  
  36. ---How can one declare a whole template class to be a friend of another
  37.    class (a template instance or any other class)?
  38.  
  39. This is precisely what I recently asked in this news group:
  40.  
  41. From saed@engn.uwindsor.ca (Saed Aryan,13325,1100,g)
  42. Subject: [Q] how declare a Template class as a friend 
  43. Date: Thu, 7 Mar 1996 22:51:36 GMT
  44.  
  45. and I have no suggestions yet...
  46.  
  47. Aryan.
  48.  
  49. //---code begins
  50. class baseA
  51.     {
  52.     friend class derB;
  53.     protected: int x;
  54.     };
  55.  
  56. class derA : public baseA {};
  57.  
  58. template <class T> class baseB {};
  59.  
  60. class derB : public baseB<derA>
  61.     {
  62.     derBMethod(baseA baseAObj){ baseAObj.x = 1; };
  63.     };
  64. //---code ends
  65.  
  66.  
  67.  
  68.